package com.chl.legou.canal.listener;

import com.alibaba.otter.canal.protocol.CanalEntry;
import com.chl.legou.canal.client.CategoryClient;
import com.chl.legou.canal.feign.PageFeign;
import com.chl.legou.item.po.Category;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xpand.starter.canal.annotation.*;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.List;

//时间监听的注解 监听数据库的变化
@CanalEventListener
public class MyEventListener {
    @Autowired
    private CategoryClient categoryClient;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private PageFeign pageFeign;

    @ListenPoint(destination = "example",
            schema = "legou1",
            table = {"spu"},
            eventType = {CanalEntry.EventType.UPDATE, CanalEntry.EventType.INSERT, CanalEntry.EventType.DELETE})
    public void onEventCustomSpu(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
        //判断操作类型
        if (eventType == CanalEntry.EventType.DELETE) {
            String spuId = "";
            List<CanalEntry.Column> beforeColumnsList = rowData.getBeforeColumnsList();
            for (CanalEntry.Column column : beforeColumnsList) {
                if (column.getName().equals("id")) {
                    spuId = column.getValue(); //spuid
                    break;
                }
            }
            // todo 删除静态页

        } else {
            //新增 或者 更新
            List<CanalEntry.Column> afterColumnsList = rowData.getAfterColumnsList();
            String spuId = "";
            for (CanalEntry.Column column : afterColumnsList) {
                if (column.getName().equals("id")) {
                    spuId = column.getValue();
                    break;
                }
            }
            //更新 生成静态页
            pageFeign.createHtml(Long.valueOf(spuId));
        }
    }


//    //当数据被添加的时候触发
//    @InsertListenPoint
//    public void onEvent(CanalEntry.EventType eventType,CanalEntry.RowData rowData){
//        System.out.println("添加数据被监听...");
//        List<CanalEntry.Column> afterColumnList = rowData.getAfterColumnsList();
//        for (CanalEntry.Column column: afterColumnList){
//            System.out.println(column.getName()+":"+column.getValue());
//        }
//    }
//
//    //当数据被修改的时候触发
//    @UpdateListenPoint
//    public void onEvent1(CanalEntry.RowData rowData){
//        System.out.println("修改数据被监听...");
//        List<CanalEntry.Column> afterColumnList = rowData.getAfterColumnsList();
//        for (CanalEntry.Column column: afterColumnList){
//            System.out.println(column.getName()+":"+column.getValue());
//        }
//    }
//
//    //当数据被删除的时候触发
//    @DeleteListenPoint
//    public void onEvent2(CanalEntry.EventType eventType,CanalEntry.RowData rowData){
//        System.out.println("删除数据被监听...");
//        List<CanalEntry.Column> afterColumnList = rowData.getAfterColumnsList();
//        for (CanalEntry.Column column: afterColumnList){
//            System.out.println(column.getName()+":"+column.getValue());
//        }
//    }

    //自定义触发时间
    @ListenPoint(destination = "example", schema = "legou1", table = {"category_"}, eventType = {CanalEntry.EventType.UPDATE, CanalEntry.EventType.INSERT, CanalEntry.EventType.DELETE})
    public void onEvent3(CanalEntry.EventType eventType, CanalEntry.RowData rowData) throws JsonProcessingException {
        List<Category> list = categoryClient.list(new Category());
        ObjectMapper objectMapper = new ObjectMapper();
        stringRedisTemplate.boundValueOps("category_list").set(objectMapper.writeValueAsString(list));

//        System.out.println("只监听分类...");
//        List<CanalEntry.Column> afterColumnList = rowData.getAfterColumnsList();
//        for (CanalEntry.Column column: afterColumnList){
//            System.out.println(column.getName()+":"+column.getValue());
//        }
    }
}
